package io.jjz.utils;

import org.apache.hadoop.hbase.util.Bytes;

/**
 * Author       : 蒋俊钊 80256973
 * Date         : 2019/3/5
 * Copyright    (C) ChinaMerchantsBank
 */
public class ByteUtil {

    private static final byte[] SPLIT = "-".getBytes();
    private static final int SPLIT_NUM = 4;

    /**
     * 拼接多个以-分隔的字节数组
     */
    public static byte[] mergerBytes(byte[]... values) {
        int length = -1; //除去最后一个空白字符
        for (byte[] value : values) {
            length += (value.length + 1);
        }

        byte[] bytes = new byte[length];
        int countLength = 0;
        for (int i = 0; i < values.length - 1; i++) {
            System.arraycopy(values[i], 0, bytes, countLength, values[i].length);
            countLength += values[i].length;
            System.arraycopy(SPLIT, 0, bytes, countLength, SPLIT.length);
            countLength += SPLIT.length;
        }
        System.arraycopy(values[values.length - 1], 0, bytes, countLength, values[values.length - 1].length);
        return bytes;
    }

    public static String getMerchNoFromBytes(byte[] bytes) {
        for (int i = 0; i < SPLIT_NUM; i++) {
            System.arraycopy(bytes, Bytes.indexOf(bytes, SPLIT) + 1,
                    bytes, 0, bytes.length - Bytes.indexOf(bytes, SPLIT) - 1);
        }
        int i = Bytes.indexOf(bytes, SPLIT);
        byte[] merch = new byte[i];
        System.arraycopy(bytes, 0, merch, 0, i);
        return new String(merch);
    }
}
